package com.employees.action;

import java.io.InputStream;

import java.text.SimpleDateFormat;


import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Hibernate;

import com.employees.bean.Employee;
import com.employees.bean.EmployeeBase;
import com.employees.bean.Employeeinfo;
import com.employees.bean.Employeeskill;
import com.employees.service.EmployeeService;

public class EmployeeAdd extends Action {
	
	private EmployeeService employeeService;
	


	public EmployeeService getEmployeeService() {
		return employeeService;
	}



	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}


	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		EmployeeBase base=(EmployeeBase) form;
		String employeeNo=base.getEmployeeNo();
		String employeeName=base.getEmployeeName();
		String employeeBirthday=base.getEmployeeBirthday();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  //譬ｼ蠑丞喧譌ｶ髣ｴ
	    Date sDate=(Date) sdf.parse(employeeBirthday); //譬ｼ蠑丞喧startDate 蠑�ｧ区慮髣ｴ荳ｺ Date邀ｻ蝙�
		String employeeSex=base.getEmployeeSex();
		String employeeDepartment=base.getEmployeeDepartment();
		InputStream stream=base.getPicture().getInputStream();
			
		
		String[]  values=request.getParameterValues("employeeSkill");
		String employeeSkill="";
		
		for(int i=0; i<values.length;i++){
			 employeeSkill+=values[i]+",";
		}
		
//		String employeeskill=request.getParameter("employeeSkill");
		String employeeSkill1=employeeSkill.substring(0, employeeSkill.length()-1);
		System.out.println("----"+employeeSkill);
		
		Employee e=new Employee();
		e.setNo(employeeNo);
		e.setName(employeeName);
		
		Employeeinfo info=new Employeeinfo();
		info.setBirth(sDate);
		info.setDepartment(employeeDepartment);
		info.setSex(employeeSex);
		info.setPhoto(Hibernate.createBlob(stream));
		info.setEmployee(e);
		
		Employeeskill skill=new Employeeskill();
		skill.setSkill(employeeSkill1);
		skill.setEmployee(e);

		employeeService.addEmployee(e);
		employeeService.addEmployee(info);
		employeeService.addEmployee(skill);
		
		return mapping.findForward("success");
	}

	
}
